package com.sxxm.qxgl.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxxm.qxgl.dto.RoleMenu_list;
import com.sxxm.qxgl.dto.SysMenu_fh;
import com.sxxm.qxgl.entity.RoleMenu;
import com.sxxm.qxgl.entity.SysDept;
import com.sxxm.qxgl.entity.SysMenu;
import com.sxxm.qxgl.entity.UserRole;
import com.sxxm.qxgl.mapper.RoleMenuMapper;
import com.sxxm.qxgl.mapper.SysMenuMapper;
import com.sxxm.qxgl.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxxm.qxgl.vo.Result;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 角色菜单权限表 服务实现类
 * </p>
 *
 * @author qjw
 * @since 2021-09-14
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    RoleMenuMapper roleMenuMapper;
    SysMenuMapper sysMenuMapper;

    public RoleMenuServiceImpl(RoleMenuMapper roleMenuMapper, SysMenuMapper sysMenuMapper) {
        this.roleMenuMapper = roleMenuMapper;
        this.sysMenuMapper = sysMenuMapper;
    }

    @Override
    public Result Batch_add(RoleMenu_list userRoles) {


        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(userRoles.getRoleId());
        String[] menuIds = userRoles.getMenuIds().split(",");
        for (int i = 0; i < menuIds.length; i++) {
            roleMenu.setMenuId(Long.valueOf(menuIds[i]).longValue());
            roleMenuMapper.insert(roleMenu);
        }
//        userRoleMapper.insert(userRoles.get(0));
        return Result.ok().message("成功").data("成功");
    }

    @Override
    public Result Delete(List<RoleMenu> userRoles) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roleId", userRoles.get(0).getRoleId());

        int sum = roleMenuMapper.deleteByMap(map);
        if (sum <= 0) {
            return Result.error().message("失败");
        }
        return Result.ok().message("成功").data("成功");
    }

    @Override
    public Result cxzfc(RoleMenu userMenu) {

//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.select("MenuId")
//                .eq("roleId", userMenu.getRoleId());
//        List<RoleMenu> zfc = roleMenuMapper.selectList(queryWrapper);
//
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < zfc.size(); i++) {
//            if (i == zfc.size() - 1) {
//                sb.append(zfc.get(i).getMenuId());
//            } else {
//                sb.append(zfc.get(i).getMenuId()).append(",");
//            }
//
//        }

        // 从数据库获取全部数据
        QueryWrapper queryWrap = new QueryWrapper();
        List<SysMenu> menus = sysMenuMapper.selectList(queryWrap);
        SysMenu_fh sysMenu_fh = new SysMenu_fh();

        for (int i = 0; i < menus.size(); i++) {
            QueryWrapper queryWraps = new QueryWrapper();
            queryWraps.eq("menuId",menus.get(i).getId());
            queryWraps.eq("roleId", userMenu.getRoleId());
            if (roleMenuMapper.selectMaps(queryWraps).size() > 0 ){
                menus.get(i).setUpdateBy("true");
            }else {
                menus.get(i).setUpdateBy("false");
            }
        }

        List<Tree<String>> treeNodes = TreeUtil.build(menus, "0",
                (treeNode, tree) -> {
                    tree.setId(String.valueOf(treeNode.getId()));
                    tree.setParentId(String.valueOf(treeNode.getParentId()));
                    tree.setWeight(treeNode.getSortCode());
                    tree.setName(treeNode.getName());
                    tree.putExtra("status", treeNode.getStatus());
                    tree.putExtra("imgUrl", treeNode.getImgUrl());
                    tree.putExtra("appUrl", treeNode.getAppUrl());
                    tree.putExtra("sortCode", treeNode.getSortCode());
                    tree.putExtra("checked", Boolean.getBoolean(treeNode.getUpdateBy()));
                });
//       sb.toString().substring(0,sb.toString().length()-1);
//        for (int i = 0; i < zfc.size(); i++) {
//
//        }

        return Result.ok().message("ww").data(treeNodes);
    }
}
